package zj.com.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.ex.DbException;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import zj.com.adapter.JcxAdapter;
import zj.com.adapter.JcxDetailAdapter;
import zj.com.app.ZjApplication;
import zj.com.db.DBUtil;
import zj.com.model.DjcModel;
import zj.com.model.InspectContent;
import zj.com.model.InspectItem;
import zj.com.model.PgScan;
import zj.com.util.DateTimePickDialogUtil;
import zj.com.util.DateUtil;
import zj.com.util.LogUtils;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/15.
 */
public class JcxDetailActivity extends  BaseActivity implements View.OnClickListener{

    public TextView title_name;
    public ImageView left_iv;

    public ListView listView;
    public List<InspectContent> list;
    public JcxDetailAdapter adapter;
    public Intent intent;

    public TextView rw_tv,yh_tv,zx_tv,jcx_tv,gs_tv,xm_tv,jc_type_tv,end_date_tv,right_tv;
    public String taskName = null;
    public RelativeLayout date_rl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jcx_detail);
        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        String  dateTime = sdf.format(calendar.getTime());
        initStartDateTime = dateTime;
        initView();
        position = getIntent().getIntExtra("position",0);
        list = new ArrayList<InspectContent>();
        adapter = new JcxDetailAdapter(list,this);
        listView.setAdapter(adapter);
    }

    public void initView(){

        date_rl = (RelativeLayout) findViewById(R.id.date_rl);
        date_rl.setOnClickListener(this);
        rw_tv = (TextView) findViewById(R.id.rw_value_tv);
        yh_tv = (TextView) findViewById(R.id.yh_value_tv);
        zx_tv = (TextView) findViewById(R.id.zx_value_tv);
        jcx_tv = (TextView) findViewById(R.id.jcx_value_tv);
        gs_tv = (TextView) findViewById(R.id.gs_value_tv);
        xm_tv = (TextView) findViewById(R.id.xm_value_tv);
        jc_type_tv = (TextView) findViewById(R.id.jc_type_value_tv);
        end_date_tv = (TextView) findViewById(R.id.end_date_value_tv);


        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String  dateTime = sdf.format(calendar.getTime());
        taskName = "检查任务-"+dateTime;
        rw_tv.setText(taskName);

        calendar.add(Calendar.DAY_OF_MONTH, 3);

        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        String  endTime = sd.format(calendar.getTime());
        end_date_tv.setText(endTime);

        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("检查单详情");
        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setVisibility(View.VISIBLE);
        left_iv.setOnClickListener(this);

        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setVisibility(View.VISIBLE);
        right_tv.setText("保存");
        right_tv.setOnClickListener(this);

        listView = (ListView) findViewById(R.id.listView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int p, long id) {

                intent = new Intent(JcxDetailActivity.this,JcdDetailEditActivity.class);
                intent.putExtra("subPosition",p);
                intent.putExtra("position",position);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        LogUtils.getInstance().error("wxf::::::onresume  "+position);
        initData();
        super.onResume();
    }

    public List<InspectContent> contents;
    public int   position;
    public InspectItem item;
    public void initData(){

        PgScan pg = ZjApplication.pgScan;
        if(pg != null){
            gs_tv.setText(pg.getUnitName());
            xm_tv.setText(pg.getProjectName());
        }


        if(ZjApplication.inspectItems == null || position > ZjApplication.inspectItems.size()) return;
        item = ZjApplication.inspectItems.get(position);
        if(item != null){
            jcx_tv.setText(item.getInspectItemName());
            jc_type_tv.setText(ZjApplication.jcd.getInspectTypeName());
            zx_tv.setText(item.getZyName());
            yh_tv.setText(item.getRiskName());

            if(item.getTaskName() != null){
                rw_tv.setText(item.getTaskName());
            }
            if(item.getEndDate() != null){
                end_date_tv.setText(DateUtil.formatStrimgDate(item.getEndDate()));
            }

           int itemId =  item.getInspectItemID();
            try {
                if(item.getContents() == null){
                    contents =  DBUtil.getInstance(this).getDBManager().selector(InspectContent.class).where("inspectitemid","=",itemId).findAll();

                }else{
                    contents = item.getContents();
                }
                item.setContents(contents);
                if(contents != null){
                    adapter.setDataChange(contents);
                }
                setListViewHeightBasedOnChildren(listView);
            } catch (DbException e) {
                e.printStackTrace();
            }


        }
    }

    private String initStartDateTime = "2013年9月3日 14:44"; // 初始化开始时间

    @Override
    public void onClick(View v) {

        switch(v.getId()){
            case R.id.left_iv:
                saveData();
                finish();
                break;
            case R.id.date_rl:
                DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil(
                        this, initStartDateTime,false);
                DateTimePickDialogUtil.isVisibleTime = false;
                dateTimePicKDialog.dateTimePicKDialog(end_date_tv);
                break;
            case R.id.right_tv:
                saveData();

                break;
        }
    }

    public void saveData(){

        String endTime = end_date_tv.getText().toString();
        if(TextUtils.isEmpty(endTime)){

            Toast.makeText(this, "请填写截止日期", Toast.LENGTH_SHORT).show();
            return;
        }


        LogUtils.getInstance().error("endtime======"+endTime);
        item.setEndDate(endTime);
        item.setTaskName(taskName);
        finish();
    }


    public void setListViewHeightBasedOnChildren(ListView listView) {

        ListAdapter listAdapter = listView.getAdapter();

        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;

        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight()+40;
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();

        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));


        listView.setLayoutParams(params);
    }
}
